/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class AccountManager {

    public boolean addAccount(Account account) {
        return accounts.add(account);
    }

    public boolean delAccount(Account account) {
        Account temp;

        for (int i = 0; i < accounts.size(); i++) {
            temp = accounts.get(i);
            if (temp == account) {
                accounts.remove(i);
                return true;
            }
        }

        return false;
    }

    public void clearAccounts() {
        accounts.clear();
    }

    public Account getAccountByID(String id) {
        Account temp;

        for (int i = 0; i < accounts.size(); i++) {
            temp = accounts.get(i);
            if (temp.getId().equals(id)) {
                return temp;
            }
        }

        return null;
    }

    

    public boolean saveToFile(String path) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        boolean retCode = false;
        try {
            fos = new FileOutputStream(path);
            oos = new ObjectOutputStream(fos);
            for (int i = 0; i < accounts.size(); ++i) {
                oos.writeObject(accounts.get(i));
            }
            retCode = true;
        } catch (Exception e) {
            System.out.println("Exception during writing file:" + e.toString());
        }

        try {
            if (oos != null) {
                oos.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e) {
            System.out.println("Exception during closing file:" + e.toString());
        }



        return retCode;
    }

    public boolean readFromFile(String path) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(path);
            ois = new ObjectInputStream(fis);

            //delete old ones
            accounts.clear();
            while (true) {
                Account temp = (Account) ois.readObject();
                accounts.add(temp);
            }

        } catch (EOFException e) {
            //end of file
            try {
                if (ois != null) {
                    ois.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException ex) {
                System.out.println("Exception during closing file:" + ex.toString());
            }
        } catch (Exception e) {
            System.out.println("Exception during serialization:" + e.toString());
            return false;
        }
        return true;
    }

    //Singleton
    public static AccountManager getInstance() {
        if (_instance == null) {
            _instance = new AccountManager();
        }
        return _instance;
    }
    private static AccountManager _instance = null;

    //Getter & Setter
    public void setAccounts(ArrayList<Account> acccounts) {
        this.accounts = acccounts;
    }

    public ArrayList<Account> getAccounts() {
        return accounts;
    }

    public void setActivatedAccount(Account activatedAccount) {
        this.activatedAccount = activatedAccount;
    }

    public Account getActivatedAccount() {
        return activatedAccount;
    }
    private ArrayList<Account> accounts = new ArrayList<Account>();
    private Account activatedAccount;
}
